# Allowed actions:
#     pass control to another generator (optionally send a value)
#     add a generator
#     remove a generator

class Coroutine(object):
    """WARNING: Incomplete code, not working!"""
    def __init__(self, *generators):
        self.generators = dict((gen.__name__, gen) for gen in generators)
        self.current = None
        self.value = None
        self.action = None
        
    def __call__(self, start):
        self.current = start
        self.value = None
        self.action = None
        while True:
            try:
                self.action = self.generators[self.current].send(self.value)
            except StopIteration:
                return
            else:
                self.value = None
                self.action.execute(self)
                
    class jump(object):
        def __init__(self, target, value=None):
            self.target = target
            self.value = value
            
        def execute(self, coroutine):
            coroutine.current = self.target
            coroutine.value = self.value
            
    class add(object):
        def __init__(self, generator, name=None):
            self.generator = generator
            self.name = generator.__name__ if name is None else name
            
        def execute(self, coroutine):
            coroutine.generators[self.name] = generator
            
    class remove(object):
        def __init__(self, name):
            self.name = name
            
        def execute(self, coroutine):
            coroutine.generators.pop(self.name)
            
